package com.common.project.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;


public class StringTools
{

	public static boolean isEmpty(String str)
	{
		return str == null || str.trim().length() == 0;
	}
	
	public static boolean isEmpty(Object obj)
	{
		return obj == null || obj.toString().length() == 0;
	}


	public static boolean isNotEmpty(String str)
	{
		return !isEmpty(str); 
	}

	public static String emptyConvert(String str, String str1)
	{
		return isNotEmpty(str) ? str : str1;
	}
	
	public static String emptyConvert(Object obj, String str1)
	{
		return obj!=null? obj.toString() : str1;
	}

	public static String trim(String str)
	{
		return str == null ? null : str.trim();
	}

	public static String replaceOnce(String text, String searchString,
			String replacement)
	{
		return replace(text, searchString, replacement, 1);
	}

	public static String replace(String text, String searchString,
			String replacement)
	{
		return replace(text, searchString, replacement, -1);
	}

	public static String replace(String text, String searchString,
			String replacement, int max)
	{
		if (isEmpty(text) || isEmpty(searchString) || replacement == null
				|| max == 0)
		{
			return text;
		}
		int start = 0;
		int end = text.indexOf(searchString, start);
		if (end == -1)
		{
			return text;
		}
		int replLength = searchString.length();
		int increase = replacement.length() - replLength;
		increase = (increase < 0 ? 0 : increase);
		increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
		StringBuffer buf = new StringBuffer(text.length() + increase);
		while (end != -1)
		{
			buf.append(text.substring(start, end)).append(replacement);
			start = end + replLength;
			if (--max == 0)
			{
				break;
			}
			end = text.indexOf(searchString, start);
		}
		buf.append(text.substring(start));
		return buf.toString();
	}

	public static boolean startsWith(String str, String prefix)
	{
		return startsWith(str, prefix, false);
	}

	public static boolean startsWithIgnoreCase(String str, String prefix)
	{
		return startsWith(str, prefix, true);
	}

	public static boolean startsWith(String str, String prefix,
			boolean ignoreCase)
	{
		return startsOrEndsWith(str, prefix, ignoreCase, false);
	}

	public static boolean endsWith(String str, String suffix)
	{
		return endsWith(str, suffix, false);
	}

	public static boolean endsWithIgnoreCase(String str, String suffix)
	{
		return endsWith(str, suffix, true);
	}

	public static boolean endsWith(String str, String prefix, boolean ignoreCase)
	{
		return startsOrEndsWith(str, prefix, ignoreCase, true);
	}

	private static boolean startsOrEndsWith(String str, String subStr,
			boolean ignoreCase, boolean endWidth)
	{
		if (str == null || subStr == null)
		{
			return (str == null && subStr == null);
		}
		if (subStr.length() > str.length())
		{
			return false;
		}
		int strOffset = 0;
		if (endWidth)
		{
			strOffset = str.length() - subStr.length();
		}
		return str.regionMatches(ignoreCase, strOffset, subStr, 0, subStr
				.length());
	}

	public static final String EMPTY = "";

	public static String join(String[] array)
	{
		return join(array, null);
	}

	public static String join(String[] list, String separator)
	{
		separator = separator == null ? EMPTY : separator;
		StringBuffer buff = new StringBuffer(5 * list.length);
		for (int i = 0; i < list.length; i++)
		{
			String s = list[i];
			if (i > 0)
			{
				buff.append(separator);
			}
			if (s != null)
			{
				buff.append(s);
			}
		}
		return buff.toString();

	}

	public static String[] split2Array(String s, char separatorChar)
	{
		return split2Array(s, separatorChar, false);
	}

	public static String[] split2Array(String s, char separatorChar,
			boolean trim)
	{
		if (s == null)
		{
			return null;
		}
		if (s.length() == 0)
		{
			return new String[0];
		}
		ArrayList list = new ArrayList();
		StringBuffer buff = new StringBuffer(s.length());
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			if (c == separatorChar)
			{
				String e = buff.toString();
				list.add(trim ? e.trim() : e);
				buff.setLength(0);
			} else if (c == '\\' && i < s.length() - 1)
			{
				buff.append(s.charAt(++i));
			} else
			{
				buff.append(c);
			}
		}
		String e = buff.toString();
		list.add(trim ? e.trim() : e);
		String[] array = new String[list.size()];
		list.toArray(array);
		return array;
	}
	/**
	 * 验证是否为null  或则 ""
	 * @param obj
	 * @return true 不为空;
	 */
	public static boolean objIsNull(Object obj){
		boolean b=false;
		if(obj!=null){
			if(isNotEmpty(obj.toString())){
				b=true;
			}
		}
		return b;
	}
	
	
	public static String catchNull(Object obj){
		return obj==null?"":obj.toString();
	}
	
	public static String catchNull(Object obj,String str){
		return obj==null?str:obj.toString();
	}
	
	//保留2位有效数字
	public static String formatDouble(double d){
		if(d==0){
			return "0.00";
		}
		DecimalFormat df = new DecimalFormat("#.00");
		if(d>0&&d<1){
			return d<1?"0"+df.format(d):df.format(d);
		}else if(d>-1&& d<0){
			return "-0"+df.format(Math.abs(d));
		}else{
			return df.format(d);
		}
		
	}
	
	/**
	 * 
	 * @param devidend 被除数
	 * @param devide  除数
	 * @return
	 */
	public static String getRate(double devidend,double devide){
		if(devide==0) return "0%";
		double d=(devidend/devide)*100;
		BigDecimal decimal=new BigDecimal(d);
		d=decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        String r=d+"%";
		return r;

    }

    /**
     * 字符串首字母小写
     *
     * @param str
     * @return
     */
    public static String toLowerCaseFirstOne(String str) {
        if (str == null || "".equals(str))
            return str;
        if (Character.isLowerCase(str.charAt(0)))
            return str;
        else
            return (new StringBuilder()).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1))
                    .toString();
    }

    /**
     * 字符串首字母大写
     *
     * @param str
     * @return
     */
    public static String toUpperCaseFirstOne(String str) {
        if (str == null || "".equals(str))
            return str;
        if (Character.isUpperCase(str.charAt(0)))
            return str;
        else
            return (new StringBuilder()).append(Character.toUpperCase(str.charAt(0))).append(str.substring(1))
                    .toString();
    }

    /**
     * 格式化手机号
     *
     * @param phone
     * @return
     */
    public static String toMobilePhone(String phone) {
        if (phone.length() == 11) {
            return phone;
        }
        if (phone.indexOf("+") > -1) {
            phone = phone.substring(3, phone.length());
        }
        if (phone.indexOf("-") > -1) {
            phone = phone.replace("-", "");
        }
        if (phone.indexOf(" ") > -1) {
            phone = phone.replace(" ", "");
        }
        return phone;
    }

	/**
	 * 生成唯一令牌
	 *
	 * @return
	 */
	public static String generateToken(String username,String passwaor) {
		String str= username+passwaor+System.currentTimeMillis();
		return MD5Util.MD5(str);
	}

	
	public static void main(String[] args) {
		System.out.println(formatDouble(10.126));
	}
	
}
